package com.starlight.flower_server.infrastructure.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.starlight.flower_server.domain.PageQuery;
import com.starlight.flower_server.domain.QuerySortType;


/**
 * @Author yfy2001
 * @date 2023/4/6 10:48
 */
public class PageUtils {

    public static <T> IPage<T> startPage(PageQuery query){
        int size = query.getSize();
        int current=query.getCurrent();
        if (size==0){
            //设置每页默认值
            size=10;
        }
        return new Page<>(current,size);
    }

    public static <T> QueryWrapper<T>  createOrderQuery(PageQuery query){
        QueryWrapper<T> queryWrapper=new QueryWrapper<>();
        String column = StringUtils.camelToUnderline(query.getColumn());
        String order = query.getOrder();
        if (QuerySortType.ASC.name().equalsIgnoreCase(order)) {
            queryWrapper.orderByAsc(column);
        } else if (QuerySortType.DESC.name().equalsIgnoreCase(order)) {
            queryWrapper.orderByDesc(column);
        }
        return queryWrapper;
    }


}
